package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.CarPermissionDao;
import com.prs.crm.domain.oa.CarPermission;

public class CarPermissionDaoImpl extends BaseDaoImpl<CarPermission> implements CarPermissionDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<CarPermission> getAllCarPermission() {
		return (List<CarPermission>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(CarPermission.class);
				return criteria.list();
			}
		});
	}

	@Override
	public CarPermission getCarPermissionByDept(Integer id) {
		Criteria criteria=this.getSession().createCriteria(CarPermission.class);
		criteria.createAlias("dept", "de");
		criteria.add(Restrictions.eq("de.id", id));
		List re=criteria.list();
		if (re==null || re.size()==0) {
			return null;
		}
		return (CarPermission) re.get(0);
	}
}
